package ue.client.planets.tiles;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public abstract class TileLoader {

	public static Image load(String path) {
		Image image = null;
		try {
			image = new Image("res/" + path);
		} catch (SlickException e) {
			e.printStackTrace();
		}
		return image;
	}
	
	/**
	 * Splits an image with a specified size.
	 * @param map BufferedImage to split.
	 * @param sizeX Number of subimages in x axis.
	 * @param sizeY Number of subimages in y axis.
     * @param width Width of a splitted image.
     * @param height Height of a splitted image.
	 * @return A BufferedImage array.
	 */
	public static Image[][] splitImage(Image image, int sizeX, int sizeY,
			int width, int height) {
		if(sizeX <= 0 || sizeY <= 0 || width <= 0 || height <= 0 || 
				sizeX * width > image.getWidth() || sizeY * height > image.getHeight()) {
			return null;
		}
		
		Image[][] images = new Image[sizeY][sizeX];

        for(int y = 0; y < sizeY; y++) {
            for(int x = 0; x < sizeX; x++) {
				images[y][x] = image.getSubImage(x * width, y * height, width, height);
			}
		}
		
		return images;
	}
	
}
